﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.order
{
    public partial class order_list : SF.Web.UI.ManagePage
    {
        Model.manager mgr;
        string appWhere;
        protected string dis = "display:none;";
        protected int totalCount;
        protected int page;
        protected int pageSize;
        //检索关键字
        protected string keywords = string.Empty;
        //支付状态
        protected string payStatus = "All";
        //支付方式
        protected string payWay = "All";
        //物流状态
        protected string expressStatus = "All";
        //结算状态
        protected string balanceStatus = "All";
        //订单状态
        protected string orderStatus = "All";
        //开始时间
        protected string startTime = string.Empty;
        //结束时间
        protected string endTime = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.keywords = DTRequest.GetQueryString("keywords");
            this.payStatus = DTRequest.GetQueryString("payStatus");
            this.payWay = DTRequest.GetQueryString("payWay");
            this.expressStatus = DTRequest.GetQueryString("expressStatus");
            this.balanceStatus = DTRequest.GetQueryString("balanceStatus");
            this.orderStatus = DTRequest.GetQueryString("orderStatus");
            this.startTime = DTRequest.GetQueryString("startTime");
            this.endTime = SF.Common.DTRequest.GetQueryString("endTime");

            this.pageSize = GetPageSize(10); //每页数量

            if (!Page.IsPostBack)
            {
                if (Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager == null)
                {
                    Response.Redirect("/admin/login.aspx");
                }
                ChkAdminLevel("order_list", DTEnums.ActionEnum.View.ToString()); //检查权限
                mgr = Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager;
                appWhere = Utils.getWhereByInfo(mgr.busnieseNum.ToString(), Session[DTKeys.SESSION_APP_INFO].ToString(), Session[DTKeys.SESSION_TEMPLATES_INFO].ToString());
                //if (string.IsNullOrEmpty(Session[DTKeys.SESSION_TEMPLATES_INFO].ToString()))
                //{
                //    JscriptMsg("请先在商品管理中选择模版！", "/admin/modeconfig/malltemplet_list.aspx");
                //    return;
                //}
                //if (new BLL.sf_modeconfig().GetModel("settingNum='" + Session[DTKeys.SESSION_TEMPLATES_INFO].ToString() + "'").marketing_type == 1)
                //{
                //    dis = "display:block;";
                //}

                RptBind(this.payStatus, this.payWay, this.expressStatus, this.balanceStatus, this.orderStatus, appWhere + " and id>0 " + CombSqlTxt(this.startTime, this.endTime, this.keywords), " createTime desc");
            }
        }

        #region 返回图文每页数量
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("order_list_page_size"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        #region 数据绑定
        private void RptBind(string _payStatus, string _payWay, string _expressStatus, string _balanceStatus, string _orderStatus, string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            if (!String.IsNullOrWhiteSpace(_payStatus))
            {
                ddlPayStatus.SelectedValue = _payStatus;
                if (!_payStatus.Equals("All"))
                {
                    _strWhere = string.Format(" payStatus='{0}' and {1}", _payStatus, _strWhere);
                }
            }
            if (!String.IsNullOrWhiteSpace(_payWay))
            {
                ddlPayWay.SelectedValue = _payWay;
                if (!_payWay.Equals("All"))
                {
                    _strWhere = string.Format(" payWay='{0}' and {1}", _payWay, _strWhere);
                }
            }
            if (!String.IsNullOrWhiteSpace(_expressStatus))
            {
                ddlExpressStatus.SelectedValue = _expressStatus;
                if (!_expressStatus.Equals("All"))
                {
                    _strWhere = string.Format(" expressStatus='{0}' and {1}", _expressStatus, _strWhere);
                }
            }
            //结算状态
            if (!String.IsNullOrWhiteSpace(_balanceStatus))
            {
                ddlBalanceStatus.SelectedValue = _balanceStatus;
                if (!_balanceStatus.Equals("All"))
                {
                    //系统确认均为未确认，则视为未结算
                    if (_balanceStatus == SF.Common.DTEnums.BalanceStatusEnum.ToBalance.ToString())
                    {
                        string toConfirmString = SF.Common.DTEnums.ConfirmStatusEnum.ToConfirm.ToString();
                        _strWhere = string.Format("sysConfirmStatus='{0}' and {1}", toConfirmString, _strWhere);
                    }
                    //系统确认均为确认，则视为已结算
                    else if (_balanceStatus == SF.Common.DTEnums.BalanceStatusEnum.DoneBalance.ToString())
                    {
                        string doneConfirmString = SF.Common.DTEnums.ConfirmStatusEnum.DoneConfirm.ToString();
                        _strWhere = string.Format("sysConfirmStatus='{0}' and {1}", doneConfirmString, _strWhere);
                    }
                }
            }
            if (!String.IsNullOrWhiteSpace(_orderStatus))
            {
                ddlOrderStatus.SelectedValue = _orderStatus;
                if (!_orderStatus.Equals("All"))
                {
                    _strWhere = string.Format(" status='{0}' and {1}", _orderStatus, _strWhere);
                }
            }
            txtStartTime.Text = this.startTime;
            txtEndTime.Text = this.endTime;
            txtKeyWords.Text = this.keywords;

            BLL.bs_good_order bll = new BLL.bs_good_order();

            this.rptList.DataSource = bll.GetModelList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();

            lblTotalMoney.Text = "0.0";
            System.Data.DataSet ds = bll.GetSumField("totalPrice", _strWhere);
            if (ds != null)
            {
                lblTotalMoney.Text = ds.Tables[0].Rows[0][0].ToString();
                if (string.IsNullOrEmpty(lblTotalMoney.Text))
                {
                    lblTotalMoney.Text = "0.0";
                }
            }
            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("order_list.aspx", "startTime={0}&endTime={1}&payStatus={2}&payWay={3}&expressStatus={4}&balanceStatus={5}&orderStatus={6}&keywords={7}&page={8}",
                this.startTime, this.endTime, this.payStatus, this.payWay, this.expressStatus, this.balanceStatus, this.orderStatus, this.keywords, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句
        protected string CombSqlTxt(string _startTime, string _endTime, string _keywords)
        {
            System.Text.StringBuilder strTemp = new System.Text.StringBuilder();
            _startTime = _startTime.Replace("'", "");
            _endTime = _endTime.Replace("'", "");
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(startTime))
            {
                strTemp.Append(" and createTime between '").Append(startTime).Append("'");
                if (!string.IsNullOrEmpty(endTime))
                    strTemp.Append(" and '").Append(endTime).Append("'");
                else
                    strTemp.Append(" and '").Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")).Append("'");
            }
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (orderNum like  '%")
                      .Append(_keywords)
                      .Append("%' or userNum like '%")
                      .Append(_keywords)
                      .Append("%' or receiveInfo like '%")
                      .Append(_keywords)
                      .Append("%') ");
            }
            return strTemp.ToString();
        }
        #endregion

        #region 筛选
        /// <summary>
        /// 支付状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlPayStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("order_list.aspx", "startTime={0}&endTime={1}&payStatus={2}&payWay={3}&expressStatus={4}&balanceStatus={5}&orderStatus={6}&keywords={7}",
                Request.Form["txtStartTime"], Request.Form["txtEndTime"], ddlPayStatus.SelectedValue, ddlPayWay.SelectedValue, ddlExpressStatus.SelectedValue, ddlBalanceStatus.SelectedValue, ddlOrderStatus.SelectedValue, txtKeyWords.Text));
        }

        /// <summary>
        /// 支付方式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlPayWay_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("order_list.aspx", "startTime={0}&endTime={1}&payStatus={2}&payWay={3}&expressStatus={4}&balanceStatus={5}&orderStatus={6}&keywords={7}",
                Request.Form["txtStartTime"], Request.Form["txtEndTime"], ddlPayStatus.SelectedValue, ddlPayWay.SelectedValue, ddlExpressStatus.SelectedValue, ddlBalanceStatus.SelectedValue, ddlOrderStatus.SelectedValue, txtKeyWords.Text));
        }

        /// <summary>
        /// 快递状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlExpressStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("order_list.aspx", "startTime={0}&endTime={1}&payStatus={2}&payWay={3}&expressStatus={4}&balanceStatus={5}&orderStatus={6}&keywords={7}",
                Request.Form["txtStartTime"], Request.Form["txtEndTime"], ddlPayStatus.SelectedValue, ddlPayWay.SelectedValue, ddlExpressStatus.SelectedValue, ddlBalanceStatus.SelectedValue, ddlOrderStatus.SelectedValue, txtKeyWords.Text));
        }

        /// <summary>
        /// 结算状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlBalanceStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("order_list.aspx", "startTime={0}&endTime={1}&payStatus={2}&payWay={3}&expressStatus={4}&balanceStatus={5}&orderStatus={6}&keywords={7}",
                Request.Form["txtStartTime"], Request.Form["txtEndTime"], ddlPayStatus.SelectedValue, ddlPayWay.SelectedValue, ddlExpressStatus.SelectedValue, ddlBalanceStatus.SelectedValue, ddlOrderStatus.SelectedValue, txtKeyWords.Text));
        }

        /// <summary>
        /// 订单状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlOrderStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("order_list.aspx", "startTime={0}&endTime={1}&payStatus={2}&payWay={3}&expressStatus={4}&balanceStatus={5}&orderStatus={6}&keywords={7}",
                Request.Form["txtStartTime"], Request.Form["txtEndTime"], ddlPayStatus.SelectedValue, ddlPayWay.SelectedValue, ddlExpressStatus.SelectedValue, ddlBalanceStatus.SelectedValue, ddlOrderStatus.SelectedValue, txtKeyWords.Text));
        }

        /// <summary>
        /// 关健字查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("order_list.aspx", "startTime={0}&endTime={1}&payStatus={2}&payWay={3}&expressStatus={4}&balanceStatus={5}&orderStatus={6}&keywords={7}",
                Request.Form["txtStartTime"], Request.Form["txtEndTime"], ddlPayStatus.SelectedValue, ddlPayWay.SelectedValue, ddlExpressStatus.SelectedValue, ddlBalanceStatus.SelectedValue, ddlOrderStatus.SelectedValue, txtKeyWords.Text));
        }

        /// <summary>
        /// 设置分页数量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("order_list_page_size", _pagesize.ToString(), 43200);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("order_list.aspx", "startTime={0}&endTime={1}&payStatus={2}&payWay={3}&expressStatus={4}&balanceStatus={5}&orderStatus={6}&keywords={7}",
                Request.Form["txtStartTime"], Request.Form["txtEndTime"], ddlPayStatus.SelectedValue, ddlPayWay.SelectedValue, ddlExpressStatus.SelectedValue, ddlBalanceStatus.SelectedValue, ddlOrderStatus.SelectedValue, txtKeyWords.Text));
        }
        #endregion


    }
}